package com.wjf.resourceservice.controller;

import com.wjf.common.entity.Result;
import com.wjf.resourceservice.feign.UserClient;
import com.wjf.resourceservice.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RestController
@RequestMapping("/api")  
public class FileController {

    @Autowired
    private FileService fileService;
    @Autowired
    private UserClient userClient;


    @PostMapping("/upload/music")
    public Result<String> uploadMusic(@RequestParam("file") MultipartFile file) {
        Result<Boolean> booleanResult = userClient.checkPermission("MUSIC:ADD");
        if (!booleanResult.getData()) {
            return Result.error(403, "没有添加音乐的权限");
        }
        try {
            String url = fileService.uploadMusic(file);
            return Result.success("上传成功！",url);
        } catch (Exception e) {

            return Result.error("上传失败！");
        }
    }

    @PostMapping("/upload/image")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            String url = fileService.uploadImage(file);
            return ResponseEntity.ok(url);
        } catch (Exception e) {
            log.error("上传图片文件失败", e);
            return ResponseEntity.badRequest().body("上传图片文件失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/upload/del")
    public Result<String> deleteFile(@RequestParam("url") String url) {
        Result<Boolean> booleanResult = userClient.checkPermission("MUSIC:DELETE");
        if (!booleanResult.getData()) {
            return Result.error(403, "没有删除音乐的权限");
        }
        try {
            fileService.deleteFile(url);
            return Result.success("文件删除成功");
        } catch (Exception e) {
            return Result.error("删除文件失败");
        }
    }
} 